﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Monitor.DAL
{
    public class GroupDAL
    {
        object objJudge = null;
        string strSql = "", strErrMsg = "";
        DataTable dt = new DataTable();

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        public GroupDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
        }

        #region 获取所有角色信息
        /// <summary>
        /// 获取所有角色信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllGrpInfo()
        {
            strSql = "select * from T_SYS_GROUP order by ID_KEY asc";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql,strErrMsg });
            return dt;
        }
        #endregion

        #region 保存新的角色信息
        /// <summary>
        /// 保存新的角色信息
        /// </summary>
        /// <returns></returns>
        public bool SaveRole(string rId, string rNameC, string rNameE)
        {
            string errMsg = "";
            string sql1 = "select * from T_SYS_GROUP where T_GRPID='" + rId + "' or T_GRPDESC_CN='" + rNameC + "' or T_GRPDESC_EN='" + rNameE + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql1, strErrMsg });
            if (dt == null || dt.Rows.Count == 0)
            {
                string sql2 = "insert into T_SYS_GROUP (T_GRPID,T_GRPDESC_CN,T_GRPDESC_EN) values ('" + rId + "','" + rNameC + "','" + rNameE + "')";
                iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql2, strErrMsg });
                if (errMsg == "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 编辑原有的角色信息
        /// <summary>
        /// 编辑原有的角色信息
        /// </summary>
        /// <returns></returns>
        public bool UpDateRole(string OrId, string rId, string rNameC, string rNameE)
        {
            string errMsg = "";
            bool flag = false;
            if (rId == OrId)
            {
                string sql1 = "select * from T_SYS_GROUP where T_GRPDESC_CN='" + rNameC + "' or T_GRPDESC_EN='" + rNameE + "'";
                dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql1, strErrMsg });
                if (dt == null || dt.Rows.Count == 0)
                {
                    string sql2 = "update T_SYS_GROUP set T_GRPDESC_CN='" + rNameC + "', T_GRPDESC_EN='" + rNameE + "' where T_GRPID='" + OrId + "'";
                    iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql2, strErrMsg });
                    if (errMsg == "")
                    {
                        flag = true;
                        
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = false;
                }
            }
            else
            {
                string sql1 = "select * from T_SYS_GROUP where T_GRPID='" + rId + "' or T_GRPDESC_CN='" + rNameC + "' or T_GRPDESC_EN='" + rNameE + "'";

                dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql1, strErrMsg });
                if (dt == null || dt.Rows.Count == 0)
                {
                    string sql2 = "update T_SYS_GROUP set T_GRPID='" + rId + "',T_GRPDESC_CN='" + rNameC + "',T_GRPDESC_EN='" + rNameE + "' where T_GRPID='" + OrId + "'";
                    iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql2, strErrMsg });
                    if (errMsg == "")
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = false;
                }
            }
            return flag;
        }
        #endregion

        #region 删除原有的角色信息
        /// <summary>
        /// 删除原有的角色信息
        /// </summary>
        /// <returns></returns>
        public bool DeleteRole(string rId)
        {
            string errMsg = "";
            bool flag = true;

            string sql = "delete from T_SYS_GROUP where T_GRPID='" + rId + "'";
            iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql, strErrMsg });

            if (errMsg == "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

    }
}
